<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>模板使用 | Python Web 框架要点</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../C07-Form/index.html" />
    
    
    <link rel="prev" href="../C06-Template/index.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="7.1"
        data-chapter-title="模板使用"
        data-filepath="C06-Template/template.md"
        data-basepath=".."
        data-revision="Sat May 05 2018 07:17:09 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Python Web 框架要点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="AboutDjango.html">
            
                
                    <a href="../AboutDjango.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Django介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="C01-CreateProject/index.html">
            
                
                    <a href="../C01-CreateProject/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        工程搭建
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="C01-CreateProject/EnvInstall.html">
            
                
                    <a href="../C01-CreateProject/EnvInstall.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        环境安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="C01-CreateProject/StartProject.html">
            
                
                    <a href="../C01-CreateProject/StartProject.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        创建工程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="C01-CreateProject/StartApp.html">
            
                
                    <a href="../C01-CreateProject/StartApp.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        创建子应用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="C01-CreateProject/CreateView.html">
            
                
                    <a href="../C01-CreateProject/CreateView.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        创建视图
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="C02-SettingsStaticAndURL/index.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        配置、静态文件与路由
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="C02-SettingsStaticAndURL/Settings.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/Settings.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        配置文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="C02-SettingsStaticAndURL/StaticFiles.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/StaticFiles.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        静态文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="C02-SettingsStaticAndURL/URL.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/URL.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        路由说明
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="C02-SettingsStaticAndURL/AppConfig.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/AppConfig.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        App应用配置
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="C03-RequestAndResponse/index.html">
            
                
                    <a href="../C03-RequestAndResponse/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        请求与响应
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="C03-RequestAndResponse/Request.html">
            
                
                    <a href="../C03-RequestAndResponse/Request.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        请求Request
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="C03-RequestAndResponse/Response.html">
            
                
                    <a href="../C03-RequestAndResponse/Response.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        响应Response
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="C03-RequestAndResponse/Cookie.html">
            
                
                    <a href="../C03-RequestAndResponse/Cookie.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        Cookie
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="C03-RequestAndResponse/Session.html">
            
                
                    <a href="../C03-RequestAndResponse/Session.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        Session
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="C04-ClassViewAndMiddleware/index.html">
            
                
                    <a href="../C04-ClassViewAndMiddleware/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        类视图与中间件
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="C04-ClassViewAndMiddleware/ClassView.html">
            
                
                    <a href="../C04-ClassViewAndMiddleware/ClassView.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        类视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="C04-ClassViewAndMiddleware/Middleware.html">
            
                
                    <a href="../C04-ClassViewAndMiddleware/Middleware.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        中间件
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="C05-Database/index.html">
            
                
                    <a href="../C05-Database/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="C05-Database/Config.html">
            
                
                    <a href="../C05-Database/Config.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="C05-Database/Model.html">
            
                
                    <a href="../C05-Database/Model.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        定义模型类
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="C05-Database/Tools.html">
            
                
                    <a href="../C05-Database/Tools.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        演示工具使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="C05-Database/CURD.html">
            
                
                    <a href="../C05-Database/CURD.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        数据库操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="C05-Database/QuerySet.html">
            
                
                    <a href="../C05-Database/QuerySet.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        查询集QuerySet
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="C05-Database/Manager.html">
            
                
                    <a href="../C05-Database/Manager.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                        管理器Manager
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="C06-Template/index.html">
            
                
                    <a href="../C06-Template/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="7.1" data-path="C06-Template/template.html">
            
                
                    <a href="../C06-Template/template.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        模板使用
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="C07-Form/index.html">
            
                
                    <a href="../C07-Form/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        表单
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="C07-Form/form.html">
            
                
                    <a href="../C07-Form/form.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        表单使用
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="9" data-path="C08-Admin/index.html">
            
                
                    <a href="../C08-Admin/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        Admin站点
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="9.1" data-path="C08-Admin/Config.html">
            
                
                    <a href="../C08-Admin/Config.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.1.</b>
                        
                        使用Admin站点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.2" data-path="C08-Admin/ModifyListDisplay.html">
            
                
                    <a href="../C08-Admin/ModifyListDisplay.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.2.</b>
                        
                        调整列表页展示
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.3" data-path="C08-Admin/ModifyEditDisplay.html">
            
                
                    <a href="../C08-Admin/ModifyEditDisplay.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.3.</b>
                        
                        调整编辑页展示
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.4" data-path="C08-Admin/ModifySite.html">
            
                
                    <a href="../C08-Admin/ModifySite.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.4.</b>
                        
                        调整站点信息
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.5" data-path="C08-Admin/UploadImage.html">
            
                
                    <a href="../C08-Admin/UploadImage.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.5.</b>
                        
                        上传图片
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >Python Web 框架要点</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x6A21;&#x677F;&#x4F7F;&#x7528;">&#x6A21;&#x677F;&#x4F7F;&#x7528;</h1>
<h2 id="1-&#x914D;&#x7F6E;">1 &#x914D;&#x7F6E;</h2>
<p>&#x5728;&#x5DE5;&#x7A0B;&#x4E2D;&#x521B;&#x5EFA;&#x6A21;&#x677F;&#x76EE;&#x5F55;templates&#x3002;</p>
<p>&#x5728;settings.py&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x4E2D;&#x4FEE;&#x6539;<strong>TEMPLATES</strong>&#x914D;&#x7F6E;&#x9879;&#x7684;DIRS&#x503C;&#xFF1A;</p>
<pre><code class="lang-python">TEMPLATES = [
    {
        <span class="hljs-string">&apos;BACKEND&apos;</span>: <span class="hljs-string">&apos;django.template.backends.django.DjangoTemplates&apos;</span>,
        <span class="hljs-string">&apos;DIRS&apos;</span>: [os.path.join(BASE_DIR, <span class="hljs-string">&apos;templates&apos;</span>)],  <span class="hljs-comment"># &#x6B64;&#x5904;&#x4FEE;&#x6539;</span>
        <span class="hljs-string">&apos;APP_DIRS&apos;</span>: <span class="hljs-keyword">True</span>,
        <span class="hljs-string">&apos;OPTIONS&apos;</span>: {
            <span class="hljs-string">&apos;context_processors&apos;</span>: [
                <span class="hljs-string">&apos;django.template.context_processors.debug&apos;</span>,
                <span class="hljs-string">&apos;django.template.context_processors.request&apos;</span>,
                <span class="hljs-string">&apos;django.contrib.auth.context_processors.auth&apos;</span>,
                <span class="hljs-string">&apos;django.contrib.messages.context_processors.messages&apos;</span>,
            ],
        },
    },
]
</code></pre>
<h2 id="2-&#x5B9A;&#x4E49;&#x6A21;&#x677F;">2 &#x5B9A;&#x4E49;&#x6A21;&#x677F;</h2>
<p>&#x5728;templates&#x76EE;&#x5F55;&#x4E2D;&#x65B0;&#x5EFA;&#x4E00;&#x4E2A;&#x6A21;&#x677F;&#x6587;&#x4EF6;&#xFF0C;&#x5982;index.html</p>
<pre><code class="lang-html"><span class="hljs-doctype">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">html</span> <span class="hljs-attribute">lang</span>=<span class="hljs-value">&quot;en&quot;</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">meta</span> <span class="hljs-attribute">charset</span>=<span class="hljs-value">&quot;UTF-8&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">title</span>&gt;</span>Title<span class="hljs-tag">&lt;/<span class="hljs-title">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">h1</span>&gt;</span>{{ city }}<span class="hljs-tag">&lt;/<span class="hljs-title">h1</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">html</span>&gt;</span>
</code></pre>
<h2 id="3-&#x6A21;&#x677F;&#x6E32;&#x67D3;">3 &#x6A21;&#x677F;&#x6E32;&#x67D3;</h2>
<p>&#x8C03;&#x7528;&#x6A21;&#x677F;&#x5206;&#x4E3A;&#x4E09;&#x6B65;&#x9AA4;&#xFF1A;</p>
<ol>
<li>&#x627E;&#x5230;&#x6A21;&#x677F;</li>
<li>&#x5B9A;&#x4E49;&#x4E0A;&#x4E0B;&#x6587;</li>
<li>&#x6E32;&#x67D3;&#x6A21;&#x677F;</li>
</ol>
<p>&#x4F8B;&#x5982;&#xFF0C;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x89C6;&#x56FE;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.http <span class="hljs-keyword">import</span> HttpResponse
<span class="hljs-keyword">from</span> django.template <span class="hljs-keyword">import</span> loader, RequestContext

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">index</span><span class="hljs-params">(request)</span>:</span>
    <span class="hljs-comment"># 1.&#x83B7;&#x53D6;&#x6A21;&#x677F;</span>
    template=loader.get_template(<span class="hljs-string">&apos;booktest/index.html&apos;</span>)
    <span class="hljs-comment"># 2.&#x5B9A;&#x4E49;&#x4E0A;&#x4E0B;&#x6587;</span>
    context=RequestContext(request,{<span class="hljs-string">&apos;city&apos;</span>: <span class="hljs-string">&apos;&#x5317;&#x4EAC;&apos;</span>})
    <span class="hljs-comment"># 3.&#x6E32;&#x67D3;&#x6A21;&#x677F;</span>
    <span class="hljs-keyword">return</span> HttpResponse(template.render(context))
</code></pre>
<p><strong>Django&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E2A;&#x51FD;&#x6570;render&#x53EF;&#x4EE5;&#x7B80;&#x5199;&#x4E0A;&#x8FF0;&#x4EE3;&#x7801;&#x3002;</strong></p>
<p>render(request&#x5BF9;&#x8C61;, &#x6A21;&#x677F;&#x6587;&#x4EF6;&#x8DEF;&#x5F84;, &#x6A21;&#x677F;&#x6570;&#x636E;&#x5B57;&#x5178;)</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.shortcuts <span class="hljs-keyword">import</span> render

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">index</span><span class="hljs-params">(request)</span>:</span>
    context={<span class="hljs-string">&apos;city&apos;</span>: <span class="hljs-string">&apos;&#x5317;&#x4EAC;&apos;</span>}
    <span class="hljs-keyword">return</span> render(request,<span class="hljs-string">&apos;index.html&apos;</span>,context)
</code></pre>
<h2 id="4--&#x6A21;&#x677F;&#x8BED;&#x6CD5;">4  &#x6A21;&#x677F;&#x8BED;&#x6CD5;</h2>
<h3 id="41-&#x6A21;&#x677F;&#x53D8;&#x91CF;">4.1 &#x6A21;&#x677F;&#x53D8;&#x91CF;</h3>
<p>&#x53D8;&#x91CF;&#x540D;&#x5FC5;&#x987B;&#x7531;&#x5B57;&#x6BCD;&#x3001;&#x6570;&#x5B57;&#x3001;&#x4E0B;&#x5212;&#x7EBF;&#xFF08;&#x4E0D;&#x80FD;&#x4EE5;&#x4E0B;&#x5212;&#x7EBF;&#x5F00;&#x5934;&#xFF09;&#x548C;&#x70B9;&#x7EC4;&#x6210;&#x3002;</p>
<p>&#x8BED;&#x6CD5;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-python">{{&#x53D8;&#x91CF;}}
</code></pre>
<p>&#x6A21;&#x677F;&#x53D8;&#x91CF;&#x53EF;&#x4EE5;&#x4F7F;python&#x7684;&#x5185;&#x5EFA;&#x7C7B;&#x578B;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x662F;&#x5BF9;&#x8C61;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">index</span><span class="hljs-params">(request)</span>:</span>
    context = {
        <span class="hljs-string">&apos;city&apos;</span>: <span class="hljs-string">&apos;&#x5317;&#x4EAC;&apos;</span>,
        <span class="hljs-string">&apos;adict&apos;</span>: {
            <span class="hljs-string">&apos;name&apos;</span>: <span class="hljs-string">&apos;&#x897F;&#x6E38;&#x8BB0;&apos;</span>,
            <span class="hljs-string">&apos;author&apos;</span>: <span class="hljs-string">&apos;&#x5434;&#x627F;&#x6069;&apos;</span>
        },
        <span class="hljs-string">&apos;alist&apos;</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>]
    }
    <span class="hljs-keyword">return</span> render(request, <span class="hljs-string">&apos;index.html&apos;</span>, context)
</code></pre>
<pre><code class="lang-html"><span class="hljs-doctype">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">html</span> <span class="hljs-attribute">lang</span>=<span class="hljs-value">&quot;en&quot;</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">meta</span> <span class="hljs-attribute">charset</span>=<span class="hljs-value">&quot;UTF-8&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">title</span>&gt;</span>Title<span class="hljs-tag">&lt;/<span class="hljs-title">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">h1</span>&gt;</span>{{ city }}<span class="hljs-tag">&lt;/<span class="hljs-title">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">h1</span>&gt;</span>{{ adict }}<span class="hljs-tag">&lt;/<span class="hljs-title">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">h1</span>&gt;</span>{{ adict.name }}<span class="hljs-tag">&lt;/<span class="hljs-title">h1</span>&gt;</span>  &#x6CE8;&#x610F;&#x5B57;&#x5178;&#x7684;&#x53D6;&#x503C;&#x65B9;&#x6CD5;
    <span class="hljs-tag">&lt;<span class="hljs-title">h1</span>&gt;</span>{{ alist }}<span class="hljs-tag">&lt;/<span class="hljs-title">h1</span>&gt;</span>  
    <span class="hljs-tag">&lt;<span class="hljs-title">h1</span>&gt;</span>{{ alist.0 }}<span class="hljs-tag">&lt;/<span class="hljs-title">h1</span>&gt;</span>  &#x6CE8;&#x610F;&#x5217;&#x8868;&#x7684;&#x53D6;&#x503C;&#x65B9;&#x6CD5;
<span class="hljs-tag">&lt;/<span class="hljs-title">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">html</span>&gt;</span>
</code></pre>
<h3 id="42-&#x6A21;&#x677F;&#x8BED;&#x53E5;">4.2 &#x6A21;&#x677F;&#x8BED;&#x53E5;</h3>
<p><strong>1&#xFF09;for&#x5FAA;&#x73AF;&#xFF1A;</strong></p>
<pre><code class="lang-python">{% <span class="hljs-keyword">for</span> item <span class="hljs-keyword">in</span> &#x5217;&#x8868; %}

&#x5FAA;&#x73AF;&#x903B;&#x8F91;
{{forloop.counter}}&#x8868;&#x793A;&#x5F53;&#x524D;&#x662F;&#x7B2C;&#x51E0;&#x6B21;&#x5FAA;&#x73AF;&#xFF0C;&#x4ECE;<span class="hljs-number">1</span>&#x5F00;&#x59CB;
{%empty%} &#x5217;&#x8868;&#x4E3A;&#x7A7A;&#x6216;&#x4E0D;&#x5B58;&#x5728;&#x65F6;&#x6267;&#x884C;&#x6B64;&#x903B;&#x8F91;

{% endfor %}
</code></pre>
<p><strong>2&#xFF09;if&#x6761;&#x4EF6;&#xFF1A;</strong></p>
<pre><code class="lang-python">{% <span class="hljs-keyword">if</span> ... %}
&#x903B;&#x8F91;<span class="hljs-number">1</span>
{% <span class="hljs-keyword">elif</span> ... %}
&#x903B;&#x8F91;<span class="hljs-number">2</span>
{% <span class="hljs-keyword">else</span> %}
&#x903B;&#x8F91;<span class="hljs-number">3</span>
{% endif %}
</code></pre>
<p>&#x6BD4;&#x8F83;&#x8FD0;&#x7B97;&#x7B26;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>==
!=
&lt;
&gt;
&lt;=
&gt;=
</code></pre><p>&#x5E03;&#x5C14;&#x8FD0;&#x7B97;&#x7B26;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>and
or
not
</code></pre><p><strong>&#x6CE8;&#x610F;&#xFF1A;&#x8FD0;&#x7B97;&#x7B26;&#x5DE6;&#x53F3;&#x4E24;&#x4FA7;&#x4E0D;&#x80FD;&#x7D27;&#x6328;&#x53D8;&#x91CF;&#x6216;&#x5E38;&#x91CF;&#xFF0C;&#x5FC5;&#x987B;&#x6709;&#x7A7A;&#x683C;&#x3002;</strong></p>
<pre><code class="lang-python">{% <span class="hljs-keyword">if</span> a == <span class="hljs-number">1</span> %}  <span class="hljs-comment"># &#x6B63;&#x786E;</span>
{% <span class="hljs-keyword">if</span> a==<span class="hljs-number">1</span> %}  <span class="hljs-comment"># &#x9519;&#x8BEF;</span>
</code></pre>
<h3 id="43-&#x8FC7;&#x6EE4;&#x5668;">4.3 &#x8FC7;&#x6EE4;&#x5668;</h3>
<p>&#x8BED;&#x6CD5;&#x5982;&#x4E0B;:</p>
<ul>
<li>&#x4F7F;&#x7528;&#x7BA1;&#x9053;&#x7B26;&#x53F7;|&#x6765;&#x5E94;&#x7528;&#x8FC7;&#x6EE4;&#x5668;&#xFF0C;&#x7528;&#x4E8E;&#x8FDB;&#x884C;&#x8BA1;&#x7B97;&#x3001;&#x8F6C;&#x6362;&#x64CD;&#x4F5C;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5728;&#x53D8;&#x91CF;&#x3001;&#x6807;&#x7B7E;&#x4E2D;&#x3002;</li>
<li><p>&#x5982;&#x679C;&#x8FC7;&#x6EE4;&#x5668;&#x9700;&#x8981;&#x53C2;&#x6570;&#xFF0C;&#x5219;&#x4F7F;&#x7528;&#x5192;&#x53F7;:&#x4F20;&#x9012;&#x53C2;&#x6570;&#x3002;</p>
<pre><code class="lang-python">&#x53D8;&#x91CF;|&#x8FC7;&#x6EE4;&#x5668;:&#x53C2;&#x6570;
</code></pre>
</li>
</ul>
<p>&#x5217;&#x4E3E;&#x51E0;&#x4E2A;&#x5982;&#x4E0B;&#xFF1A;</p>
<ul>
<li><strong>safe</strong>&#xFF0C;&#x7981;&#x7528;&#x8F6C;&#x4E49;&#xFF0C;&#x544A;&#x8BC9;&#x6A21;&#x677F;&#x8FD9;&#x4E2A;&#x53D8;&#x91CF;&#x662F;&#x5B89;&#x5168;&#x7684;&#xFF0C;&#x53EF;&#x4EE5;&#x89E3;&#x91CA;&#x6267;&#x884C;</li>
</ul>
<ul>
<li><strong>length</strong>&#xFF0C;&#x957F;&#x5EA6;&#xFF0C;&#x8FD4;&#x56DE;&#x5B57;&#x7B26;&#x4E32;&#x5305;&#x542B;&#x5B57;&#x7B26;&#x7684;&#x4E2A;&#x6570;&#xFF0C;&#x6216;&#x5217;&#x8868;&#x3001;&#x5143;&#x7EC4;&#x3001;&#x5B57;&#x5178;&#x7684;&#x5143;&#x7D20;&#x4E2A;&#x6570;&#x3002;</li>
<li><p><strong>default</strong>&#xFF0C;&#x9ED8;&#x8BA4;&#x503C;&#xFF0C;&#x5982;&#x679C;&#x53D8;&#x91CF;&#x4E0D;&#x5B58;&#x5728;&#x65F6;&#x5219;&#x8FD4;&#x56DE;&#x9ED8;&#x8BA4;&#x503C;&#x3002;</p>
<pre><code>data|default:&apos;&#x9ED8;&#x8BA4;&#x503C;&apos;
</code></pre></li>
<li><p><strong>date</strong>&#xFF0C;&#x65E5;&#x671F;&#xFF0C;&#x7528;&#x4E8E;&#x5BF9;&#x65E5;&#x671F;&#x7C7B;&#x578B;&#x7684;&#x503C;&#x8FDB;&#x884C;&#x5B57;&#x7B26;&#x4E32;&#x683C;&#x5F0F;&#x5316;&#xFF0C;&#x5E38;&#x7528;&#x7684;&#x683C;&#x5F0F;&#x5316;&#x5B57;&#x7B26;&#x5982;&#x4E0B;&#xFF1A;</p>
<ul>
<li>Y&#x8868;&#x793A;&#x5E74;&#xFF0C;&#x683C;&#x5F0F;&#x4E3A;4&#x4F4D;&#xFF0C;y&#x8868;&#x793A;&#x4E24;&#x4F4D;&#x7684;&#x5E74;&#x3002;</li>
<li>m&#x8868;&#x793A;&#x6708;&#xFF0C;&#x683C;&#x5F0F;&#x4E3A;01,02,12&#x7B49;&#x3002;</li>
<li>d&#x8868;&#x793A;&#x65E5;, &#x683C;&#x5F0F;&#x4E3A;01,02&#x7B49;&#x3002;</li>
<li>j&#x8868;&#x793A;&#x65E5;&#xFF0C;&#x683C;&#x5F0F;&#x4E3A;1,2&#x7B49;&#x3002;</li>
<li>H&#x8868;&#x793A;&#x65F6;&#xFF0C;24&#x8FDB;&#x5236;&#xFF0C;h&#x8868;&#x793A;12&#x8FDB;&#x5236;&#x7684;&#x65F6;&#x3002;</li>
<li>i&#x8868;&#x793A;&#x5206;&#xFF0C;&#x4E3A;0-59&#x3002;</li>
<li>s&#x8868;&#x793A;&#x79D2;&#xFF0C;&#x4E3A;0-59&#x3002;</li>
</ul>
<pre><code>value|date:&quot;Y&#x5E74;m&#x6708;j&#x65E5;  H&#x65F6;i&#x5206;s&#x79D2;&quot;
</code></pre></li>
</ul>
<h3 id="44-&#x6CE8;&#x91CA;">4.4 &#x6CE8;&#x91CA;</h3>
<p>1&#xFF09;&#x5355;&#x884C;&#x6CE8;&#x91CA;&#x8BED;&#x6CD5;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>{#...#}
</code></pre><p>2&#xFF09;&#x591A;&#x884C;&#x6CE8;&#x91CA;&#x4F7F;&#x7528;comment&#x6807;&#x7B7E;&#xFF0C;&#x8BED;&#x6CD5;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-python">{% comment %}
...
{% endcomment %}
</code></pre>
<h3 id="45-&#x6A21;&#x677F;&#x7EE7;&#x627F;">4.5 &#x6A21;&#x677F;&#x7EE7;&#x627F;</h3>
<p>&#x6A21;&#x677F;&#x7EE7;&#x627F;&#x548C;&#x7C7B;&#x7684;&#x7EE7;&#x627F;&#x542B;&#x4E49;&#x662F;&#x4E00;&#x6837;&#x7684;&#xFF0C;&#x4E3B;&#x8981;&#x662F;&#x4E3A;&#x4E86;&#x63D0;&#x9AD8;&#x4EE3;&#x7801;&#x91CD;&#x7528;&#xFF0C;&#x51CF;&#x8F7B;&#x5F00;&#x53D1;&#x4EBA;&#x5458;&#x7684;&#x5DE5;&#x4F5C;&#x91CF;&#x3002;</p>
<p><strong>&#x7236;&#x6A21;&#x677F;</strong></p>
<p>&#x5982;&#x679C;&#x53D1;&#x73B0;&#x5728;&#x591A;&#x4E2A;&#x6A21;&#x677F;&#x4E2D;&#x67D0;&#x4E9B;&#x5185;&#x5BB9;&#x76F8;&#x540C;&#xFF0C;&#x90A3;&#x5C31;&#x5E94;&#x8BE5;&#x628A;&#x8FD9;&#x6BB5;&#x5185;&#x5BB9;&#x5B9A;&#x4E49;&#x5230;&#x7236;&#x6A21;&#x677F;&#x4E2D;&#x3002;</p>
<p>&#x6807;&#x7B7E;block&#xFF1A;&#x7528;&#x4E8E;&#x5728;&#x7236;&#x6A21;&#x677F;&#x4E2D;&#x9884;&#x7559;&#x533A;&#x57DF;&#xFF0C;&#x7559;&#x7ED9;&#x5B50;&#x6A21;&#x677F;&#x586B;&#x5145;&#x5DEE;&#x5F02;&#x6027;&#x7684;&#x5185;&#x5BB9;&#xFF0C;&#x540D;&#x5B57;&#x4E0D;&#x80FD;&#x76F8;&#x540C;&#x3002; &#x4E3A;&#x4E86;&#x66F4;&#x597D;&#x7684;&#x53EF;&#x8BFB;&#x6027;&#xFF0C;&#x5EFA;&#x8BAE;&#x7ED9;endblock&#x6807;&#x7B7E;&#x5199;&#x4E0A;&#x540D;&#x5B57;&#xFF0C;&#x8FD9;&#x4E2A;&#x540D;&#x5B57;&#x4E0E;&#x5BF9;&#x5E94;&#x7684;block&#x540D;&#x5B57;&#x76F8;&#x540C;&#x3002;&#x7236;&#x6A21;&#x677F;&#x4E2D;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E0A;&#x4E0B;&#x6587;&#x4E2D;&#x4F20;&#x9012;&#x8FC7;&#x6765;&#x7684;&#x6570;&#x636E;&#x3002;</p>
<pre><code class="lang-python">{% block &#x540D;&#x79F0; %}
&#x9884;&#x7559;&#x533A;&#x57DF;&#xFF0C;&#x53EF;&#x4EE5;&#x7F16;&#x5199;&#x9ED8;&#x8BA4;&#x5185;&#x5BB9;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x6CA1;&#x6709;&#x9ED8;&#x8BA4;&#x5185;&#x5BB9;
{% endblock  &#x540D;&#x79F0; %}
</code></pre>
<p><strong>&#x5B50;&#x6A21;&#x677F;</strong></p>
<p>&#x6807;&#x7B7E;extends&#xFF1A;&#x7EE7;&#x627F;&#xFF0C;&#x5199;&#x5728;&#x5B50;&#x6A21;&#x677F;&#x6587;&#x4EF6;&#x7684;&#x7B2C;&#x4E00;&#x884C;&#x3002;</p>
<pre><code>{% extends &quot;&#x7236;&#x6A21;&#x677F;&#x8DEF;&#x5F84;&quot;%}
</code></pre><p>&#x5B50;&#x6A21;&#x7248;&#x4E0D;&#x7528;&#x586B;&#x5145;&#x7236;&#x6A21;&#x7248;&#x4E2D;&#x7684;&#x6240;&#x6709;&#x9884;&#x7559;&#x533A;&#x57DF;&#xFF0C;&#x5982;&#x679C;&#x5B50;&#x6A21;&#x7248;&#x6CA1;&#x6709;&#x586B;&#x5145;&#xFF0C;&#x5219;&#x4F7F;&#x7528;&#x7236;&#x6A21;&#x7248;&#x5B9A;&#x4E49;&#x7684;&#x9ED8;&#x8BA4;&#x503C;&#x3002;</p>
<p>&#x586B;&#x5145;&#x7236;&#x6A21;&#x677F;&#x4E2D;&#x6307;&#x5B9A;&#x540D;&#x79F0;&#x7684;&#x9884;&#x7559;&#x533A;&#x57DF;&#x3002;</p>
<pre><code>{% block &#x540D;&#x79F0; %}
&#x5B9E;&#x9645;&#x586B;&#x5145;&#x5185;&#x5BB9;
{{ block.super }}&#x7528;&#x4E8E;&#x83B7;&#x53D6;&#x7236;&#x6A21;&#x677F;&#x4E2D;block&#x7684;&#x5185;&#x5BB9;
{% endblock &#x540D;&#x79F0; %}
</code></pre>
                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../C06-Template/index.html" class="navigation navigation-prev " aria-label="Previous page: 模板"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../C07-Form/index.html" class="navigation navigation-next " aria-label="Next page: 表单"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
